package com.link.base.base.project.controller;

import com.link.base.base.project.model.Project;
import com.link.base.base.project.model.QuoteRound;
import com.link.base.base.project.service.ProjectService;
import com.link.base.base.project.service.QuoteRoundService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.RedisUtil;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 报价轮次表
 *
 * @author 王昊
 * @date 2020-10-13 16:40:05
 */
@Controller
@RequestMapping("/link/quoteRound")
public class QuoteRoundController extends BasicController<QuoteRound> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private QuoteRoundService quoteRoundService;

    @Resource
    private ProjectService projectService;

    @Override
    public BasicService<QuoteRound> getBasicService() throws Exception {
        return quoteRoundService;
    }


    @RequestMapping(value = "/compute", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> compute(@JsonParam QuoteRound entity, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        LinkRedisClient redisClient = null;
        try {
            if (null == entity.getProjectHeadId()) {
                throw new RuntimeException("项目id不能为空!");
            }
            redisClient = RedisUtil.getJedis();
            String quoteRoundRedisKey = "LINK_QUOTE_ROUND_" + entity.getProjectHeadId();
            boolean isExcutingFlag = redisClient.exists(quoteRoundRedisKey);
            if (isExcutingFlag) {
                throw new Exception("当前报价计算中，请稍后重新计算!");
            }
            Project project = projectService.queryById(entity.getProjectHeadId());
            if ("Y".equals(project.getIsQuoteCompute())) {
                throw new Exception("当前项目已计算完成，不能重复计算!");
            }
            // 马上加锁控制
            redisClient.setex(quoteRoundRedisKey, 20, "1");// 3 分钟
            quoteRoundService.compute(entity);
            // 计算完成，释放锁
            redisClient.del(quoteRoundRedisKey);
            result.put("newRow",entity);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        } finally {
            if (redisClient != null) {
                RedisUtil.returnResource(redisClient);
            }
        }
        return result;
    }
}
